package com.zlx.sharelive.activity;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.RankingBean;
import com.zlx.module_base.base_api.res_data.RankingItemBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;
import com.zlx.sharelive.utils.NullTokenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 排行榜
 */
public class RankingAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;
    @BindView(R.id.tv_check1)
    TextView tv_check1;
    @BindView(R.id.tv_check2)
    TextView tv_check2;
    @BindView(R.id.tv_check3)
    TextView tv_check3;
    @BindView(R.id.tv_type1)
    TextView tv_type1;
    @BindView(R.id.tv_type2)
    TextView tv_type2;
    @BindView(R.id.tv_type3)
    TextView tv_type3;
    @BindView(R.id.view_type1)
    View view_type1;
    @BindView(R.id.view_type2)
    View view_type2;
    @BindView(R.id.view_type3)
    View view_type3;

    private List<TextView> mCheckList = new ArrayList<>();
    private List<TextView> mTypeList = new ArrayList<>();
    private List<View> mTypeViewList = new ArrayList<>();

    private String check = "1";
    private String type = "3";
    private List<RankingItemBean> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<RankingItemBean> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_ranking;
    }

    @Override
    public void initViews() {
        super.initViews();

        mCheckList.clear();
        mCheckList.add(tv_check1);
        mCheckList.add(tv_check2);
        mCheckList.add(tv_check3);
        mTypeList.clear();
        mTypeList.add(tv_type1);
        mTypeList.add(tv_type2);
        mTypeList.add(tv_type3);
        mTypeViewList.clear();
        mTypeViewList.add(view_type1);
        mTypeViewList.add(view_type2);
        mTypeViewList.add(view_type3);

        getRanking();
        initRecyclerView();
    }

    /**
     * 排行榜
     */
    private void getRanking() {
        ApiUtil.getHomeApi().getRanking(type).observe(this,
                new BaseObserver<>(new BaseObserverCallBack<ApiResponse<RankingBean>>() {
                    @Override
                    public void onSuccess(ApiResponse<RankingBean> data) {
                        mList.clear();
                        if (null != data) {
                            if (null != data.getData()) {
                                if (data.getData().getCode() == 0) {
                                    if (null != data.getData().getInfo()) {
                                        if (data.getData().getInfo().size() > 0) {
                                            if (check.equals("1")) {
                                                mList.addAll(data.getData().getInfo().get(0).getMoney());
                                            } else if (check.equals("2")) {
                                                mList.addAll(data.getData().getInfo().get(0).getWatch());
                                            } else if (check.equals("3")) {
                                                mList.addAll(data.getData().getInfo().get(0).getExperience());
                                            }
                                        }
                                    }
                                } else {
                                    NullTokenUtils.isNullToken(data.getData().getCode(), data.getData().getMsg());
                                }
                            }
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFail(String msg) {
                        super.onFail(msg);

                    }
                }));
    }

    /**
     * 初始化数据列表
     */
    private void initRecyclerView() {
        rv_data.setLayoutManager(new LinearLayoutManager(this));

        mAdapter = new CommonRecyclerViewAdapter<RankingItemBean>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, RankingItemBean entity, int position) {
                ImageView iv_ranking_head = holder.getView(R.id.iv_ranking_head);
                GlideImgManager.glideLoader(RankingAc.this, entity.getAvatar(), iv_ranking_head, 0);
                if (check.equals("1")) {
                    holder.setText(R.id.tv_ranking_money, "￥" + entity.getCount());
                } else if (check.equals("2")) {
                    holder.setText(R.id.tv_ranking_money, "人气" + entity.getCount());
                } else if (check.equals("3")) {
                    holder.setText(R.id.tv_ranking_money, "活跃" + entity.getCount());
                }

                holder.setText(R.id.tv_ranking_nickname, entity.getNickName());

                if (entity.getIsVip().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_vip)).setVisibility(View.VISIBLE);
                } else {
                    ((ImageView) holder.getView(R.id.iv_vip)).setVisibility(View.GONE);
                }

                if (entity.getAuthentication().equals("1")) {
                    ((ImageView) holder.getView(R.id.iv_goddess)).setVisibility(View.VISIBLE);
                } else {
                    ((ImageView) holder.getView(R.id.iv_goddess)).setVisibility(View.GONE);
                }

                Button bt_number = holder.getView(R.id.bt_number);
                switch (position) {
                    case 0:
                        bt_number.setText("");
                        bt_number.setBackgroundResource(R.mipmap.no_1_img);
                        break;
                    case 1:
                        bt_number.setText("");
                        bt_number.setBackgroundResource(R.mipmap.no_2_img);
                        break;
                    case 2:
                        bt_number.setText("");
                        bt_number.setBackgroundResource(R.mipmap.no_3_img);
                        break;
                    default:
                        bt_number.setBackgroundResource(0);
                        bt_number.setText(String.valueOf(position + 1));
                        break;
                }
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_ranking;
            }
        };

        rv_data.setAdapter(mAdapter);
    }


    @OnClick({R.id.iv_back, R.id.ll_type1, R.id.ll_type2, R.id.ll_type3
            , R.id.tv_check1, R.id.tv_check2, R.id.tv_check3})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_type1:
                type = "3";
                getRanking();
                setTypeColor(0);
                break;
            case R.id.ll_type2:
                type = "2";
                getRanking();
                setTypeColor(1);
                break;
            case R.id.ll_type3:
                type = "1";
                getRanking();
                setTypeColor(2);
                break;
            case R.id.tv_check1:
                check = "1";
                getRanking();
                setCheckColor(0);
                break;
            case R.id.tv_check2:
                check = "2";
                getRanking();
                setCheckColor(1);
                break;
            case R.id.tv_check3:
                check = "3";
                getRanking();
                setCheckColor(2);
                break;
        }
    }

    public void setCheckColor(int position) {
        for (int i = 0; i < mCheckList.size(); i++) {
            if (i == position) {
                mCheckList.get(position).setTextColor(getResources().getColor(R.color.white));
            } else {
                mCheckList.get(i).setTextColor(getResources().getColor(R.color.white_alpha_38));
            }
        }
    }

    public void setTypeColor(int position) {
        for (int i = 0; i < mTypeList.size(); i++) {
            if (i == position) {
                mTypeList.get(position).setTextColor(getResources().getColor(R.color.white));
                mTypeViewList.get(position).setVisibility(View.VISIBLE);
            } else {
                mTypeList.get(i).setTextColor(getResources().getColor(R.color.white_alpha_38));
                mTypeViewList.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }
}
